package com.idcheck.rechcustoms;

import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import utils.LogUtil;
import utils.OtherUtil;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import static android.content.ContentValues.TAG;
import static com.idcheck.rechcustoms.SerialPortWorker.serialObjectLock;


public class QueueService {
    private static Map<String,JSONObject> sendBuffer = new HashMap<>(0, 1);
    private static QueueService singletonQueueService = new QueueService();
    //将构造器设置为private禁止通过new进行实例化
    private QueueService() {

    }
    public static QueueService getInstance() {
        return singletonQueueService;
    }

    public static boolean send(String cmd,JSONObject obj,int IO){
        cmd = cmd.toLowerCase();

        //新型柜子不论什么类型都需要同时开大门
        String finalCmd = cmd;
        sendBuffer.put(cmd,obj);

        QueueService.getInstance().sendNotify(finalCmd);
        //OtherUtil.OpenVoice7(OtherUtil.getMainContext());
        QueueService.getInstance().sendNotify("900300fa1aa0");
//        runOnUiThread(new Runnable() {
//            @Override
//            public void run() {
//                Handler handler = new Handler();
//                handler.postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        QueueService.getInstance().sendNotify("900300fa1aa0");
//                    }
//                }, 1000);
//            }
//        });

        /*if (IO == 0) {
            String a = cmd.substring(0, 4);
            String b = cmd.substring(4, 6);
            String c = cmd.substring(6, 8);
            String d = cmd.substring(8, 11);
            cmd = a + c + b + d;
        }
        sendBuffer.put(cmd,obj);*/

        /*if (cmd.substring(4, 6).equals("00")) {
            //如果是袋子类型的，需要同时开大门

        }*/
        return true;
    }
    public boolean sendNotify(String cmd){
        try{
            synchronized (serialObjectLock) {
                serialObjectLock.setArg(cmd);
                serialObjectLock.notify();
            }
        }catch (Exception e){
            Log.d(TAG, e.toString());
        }

        return true;
    }
//    public static boolean recvNotify(String cmd){
//        try{
//            //Mode 为自定义数据名称
//            SharedPreferences share = MainActivity.MainContext.getSharedPreferences("Mode",MainActivity.MainContext.MODE_MULTI_PROCESS );
//            SharedPreferences.Editor editor = share.edit();
//            editor.putString("key", cmd);
//            editor.apply();
//        }catch (Exception e){
//            Log.d(TAG, e.toString());
//        }
//        return true;
//    }

    public static void remove(String cmd){
        try{

            sendBuffer.remove(cmd);
        }catch (Exception e){

        }
    }
    public static boolean recv(String cmd){
        try{
            StringBuffer tcmd = new StringBuffer(cmd);
            tcmd.setCharAt(8,'1');
            String rCmd = tcmd.toString();
            if(sendBuffer.containsKey(rCmd)){
                JSONObject obj = sendBuffer.get(rCmd);
                obj.getJSONObject("data").put("ret",rCmd);
            }
        }catch (Exception e){
            return false;
        }

        return true;
    }

    //1a发送确认
    public static boolean recv1a(String cmd){
        try{
            StringBuffer tcmd = new StringBuffer(cmd);
            tcmd.setCharAt(8,'1');
            tcmd.setCharAt(9,'a');
            String rCmd = tcmd.toString();
            LogUtil.getInstance().info(OtherUtil.getCurrentTime()+" RECV1A:"+rCmd);
            if(sendBuffer.containsKey(rCmd)){
                JSONObject obj = sendBuffer.get(rCmd);
                obj.getJSONObject("data").put("ret",rCmd);
                obj.getJSONObject("data").put("status","2");


            }

        }catch (Exception e){
            return false;
        }

        return true;
    }

    //3a成功确认
    public static boolean recv3a(String cmd){
        try{
            StringBuffer tcmd = new StringBuffer(cmd);
            tcmd.setCharAt(8,'1');
            tcmd.setCharAt(9,'a');
            String rCmd = tcmd.toString();
            LogUtil.getInstance().info(OtherUtil.getCurrentTime()+" =======3CMD:"+rCmd);
            if(sendBuffer.containsKey(rCmd)){
                JSONObject obj = sendBuffer.get(rCmd);
                obj.getJSONObject("data").put("ret",rCmd);
                obj.getJSONObject("data").put("status","4");
                obj.getJSONObject("data").put("retime",System.currentTimeMillis());

                String type = obj.getJSONObject("data").getString("type");
                /*if (type.equals("CMD_RED_OPEN_GRID")) {
                    type = "RE_"+ type;
                } else {
                    //这边除了红外扫描开柜统一回复一个type，不做开柜指令来源判断
                    type = "RE_LOCAL_UQJ_OPEN_GRID";
                }*/

                type = "RE_"+ type;

                obj.getJSONObject("data").put("type",type);
                LogUtil.getInstance().info(OtherUtil.getCurrentTime()+" ========3RET:"+obj.toString());
                int _io=obj.getJSONObject("data").getInt("IO");
                int _485=obj.getJSONObject("data").getInt("485");
                //returnServerSuccess(rCmd,obj.toString());
                Message message = new Message();
                message.what=10005;
                message.obj=obj.toString();
                EventBus.getDefault().post(message);
                sendBuffer.remove(rCmd);


            }



        }catch (Exception e){
            return false;
        }

        return true;
    }

    //4a失败确认
    public static boolean recv4a(String cmd){
        try{
            StringBuffer tcmd = new StringBuffer(cmd);
            tcmd.setCharAt(8,'1');
            tcmd.setCharAt(9,'a');
            String rCmd = tcmd.toString();
            LogUtil.getInstance().info(OtherUtil.getCurrentTime()+" =======4CMD:"+rCmd);
            if(sendBuffer.containsKey(rCmd)){
                JSONObject obj = sendBuffer.get(rCmd);
                obj.getJSONObject("data").put("ret",rCmd);
                obj.getJSONObject("data").put("status","4");
                obj.getJSONObject("data").put("retime",System.currentTimeMillis());
                //String type = obj.getString("type");
                String type = obj.getJSONObject("data").getString("type");
                type = "RE_"+ type;
                //obj.put("type",type);
                obj.getJSONObject("data").put("type",type);
                LogUtil.getInstance().info(OtherUtil.getCurrentTime()+" ========4RET:"+obj.toString());
                int _io=obj.getJSONObject("data").getInt("IO");
                int _485=obj.getJSONObject("data").getInt("485");
                //returnServerFailed(rCmd,obj.toString());
                Message message = new Message();
                message.what=10005;
                message.obj=obj.toString();
                EventBus.getDefault().post(message);
                sendBuffer.remove(rCmd);

            }
        }catch (Exception e){
            return false;
        }

        return true;
    }

    //1c信息确认
    public static boolean recv1c(String cmd){
        try{
            StringBuffer tcmd = new StringBuffer(cmd);
            tcmd.setCharAt(8,'1');
            tcmd.setCharAt(9,'a');
            String rCmd = tcmd.toString();
            if(sendBuffer.containsKey(rCmd)){
                JSONObject obj = sendBuffer.get(rCmd);
                obj.getJSONObject("data").put("ret",rCmd);
                obj.getJSONObject("data").put("status","8");
            }
            //recvNotify(rCmd);
        }catch (Exception e){
            return false;
        }

        return true;
    }

}
